package cn.cvs.config;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUservice;
import com.sun.xml.internal.ws.util.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

@Controller
@RequestMapping("/user")
public class SysUserController {
    private Logger logger = Logger.getLogger(String.valueOf(SysUserController.class));
    private ArrayList<SysUser>sysUserList = new ArrayList<SysUser>();
    private ArrayList<SysUser>queryUserList = new ArrayList<SysUser>();

    @Resource
    private SysUservice sysUservice;
    @RequestMapping(value = "/toLogin")
    public String login(){
        logger.info("跳转到登录页面");
        return "login";
    }
    @PostMapping("login")
    public String doLogin(HttpServletRequest request, HttpSession session,@RequestParam String account,@RequestParam String password){
        logger.info("登录方法");
        SysUser sysUser = sysUservice.login(account,password);
        if (null!=sysUser){
            session.setAttribute(Constants.USER_SESSION,sysUser);
            return "redirect:user/toMain";
        }else {
            request.setAttribute("error","用户名或密码不正确");
            return "login";
        }
    }
    @PostMapping("/toMain")
    public String main(HttpSession session){
        if (session.getAttribute(Constants.USER_SESSION)==null)
        return "redirect:/user/toLogin";
        return "frame";
    }
    public SysUserController(){
        sysUserList.add(new SysUser(5,"zhaojing","赵静","55555555",1,"13054784445",
                "上海市宝山区",1,1,new Date(),1,new Date()));
    }
    @GetMapping("/list")
    public String list(Model model){

        logger.info("当查询条件为空时,查询用户信息");
        queryUserList.clear();
        queryUserList.addAll(sysUserList);
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }
    @PostMapping("/list")
    public String list(@RequestParam(value = "realName",required = false)String realName,Model model){
        logger.info("查询条件:【realName】="+realName+",查询用户信息");
        queryUserList.clear();
        if (realName!=null && !realName.equals("")){
            for (SysUser user :sysUserList){
                if (user.getRealName().indexOf(realName)!=-1){
                    queryUserList.add(user);
                }
            }
        }else {
            queryUserList.addAll(sysUserList);
        }
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }
    @RequestMapping(value = "/toSysError")
    public String sysError(){
        return "sysError";
    }
}
